package vcp.client.GUI.mvc.controller;

import javax.swing.SwingUtilities;

import vcp.client.GUI.events.FormSubmitionEvent;
import vcp.client.GUI.mvc.model.UIAbstractModel;
import vcp.client.GUI.mvc.model.UIAccStatusModel;
import vcp.client.GUI.mvc.view.UIAbstractView;
import vcp.client.GUI.mvc.view.UIAccStatusView;
import vcp.client.forms.SubscriptionCancelForm;
import vcp.client.interfaces.listeners.IFormSubmitionListener;
import vcp.client.utility.Common.UserNotification;
import vcp.entity.Customer;
import vcp.entity.Subscription;
import vcp.enums.PopUpMessageType;
import vcp.enums.Screen;

/**
 * 
 * @author VCP team
 *         <p>
 *         account status controller
 *         </p>
 * 
 * @see UIAccStatusView
 * @see UIAccStatusModel
 * @see IFormSubmitionListener
 */
public class UIAccStatusController extends UIAbstractController implements IFormSubmitionListener <SubscriptionCancelForm>
{
	/** reference to the view **/
	private final UIAccStatusView view;
	/** reference to the model **/
	private final UIAccStatusModel model;

	/**
	 * 
	 * @param view
	 *            screen to the view
	 * @param model
	 *            data from view
	 */
	public UIAccStatusController ( UIAccStatusView view, UIAccStatusModel model )
	{
		super(Screen.ACCOUNT_STATUS);
		this.model = model;
		this.view = view;
		this.setupListeners();
	}

	// ****************************************************************************************************************************
	// Private methods
	// ****************************************************************************************************************************
	/**
	 * update data on the view
	 */
	private void reloadGUIRequestedSubscription ()
	{
		Subscription subscriptionInfo = this.model.getRequestedSubscription(this.view.getCurrentSubscription());
		this.view.loadOneSubscription(subscriptionInfo);
	}

	// ****************************************************************************************************************************
	// UIAbstractController methods
	// ****************************************************************************************************************************
	@Override
	public UIAbstractView getView ()
	{
		return this.view;
	}

	@Override
	public UIAbstractModel getModel ()
	{
		return this.model;
	}

	@Override
	public void clearMVC ()
	{
		this.view.setOnScreen(false);
	}

	@Override
	public void flashData ()
	{
		this.view.clearView();
		this.model.clearModel();
	}

	@Override
	public void uploadDataContext ( Object data )
	{
		if ( data instanceof Customer )
		{
			this.flashData();

			Customer customer = (Customer) data;
			this.model.setCustomer(customer);

			this.reloadGUI();
			this.model.shouldRegisterForUpdates();
			this.view.setOnScreen(true);
		}
	}

	@Override
	protected synchronized void reloadGUI ()
	{
		this.view.setOnScreen(false);

		this.view.loadSubscriptions(this.model.getCustomer().getSubscriptions());
		this.view.loadBalance(this.model.getCustomer().getBalance());

		this.view.setOnScreen(true);
	}

	@Override
	protected void setupListeners ()
	{
		super.setupListeners();
		this.view.setFormSubmtionListener(this);
	}

	// ****************************************************************************************************************************
	// IControllerViewDelegate methods
	// ****************************************************************************************************************************
	@Override
	public void onButtonPress ( Object object )
	{
		this.reloadGUIRequestedSubscription();
	}

	@Override
	public void updateControllerDelegateWithData ( Object newData )
	{
		this.parentDelegate.notifyOnDataUpdate(this.model.getCustomer());
		if ( this.view.isOnScreen() )
		{
			this.parentDelegate.showPopUpMessage(String.format(UserNotification.__VALUE__IS_UPDATED, "Your account "), PopUpMessageType.WARNING);

			SwingUtilities.invokeLater(new Runnable()
				{
					@Override
					public void run ()
					{
						UIAccStatusController.this.reloadGUI();
					}
				});
		}
	}

	// ****************************************************************************************************************************
	// IFormSubmitionListener methods
	// ****************************************************************************************************************************
	@Override
	public void onFormSubmitted ( FormSubmitionEvent <SubscriptionCancelForm> event )
	{
		if ( event.getForm() != null )
		{
			SubscriptionCancelForm form = event.getForm();
			Subscription subscription = this.model.getRequestedSubscription(form.getSubscriptionID());
			form.setSubscription(subscription);
			this.model.cancelSubscription(form);
		}
		else
		{
			this.parentDelegate.showPopUpMessage(UserNotification.WRONG_DATA, PopUpMessageType.ERROR);
		}
	}
}
